package com.example.index

import android.os.Parcel
import android.os.Parcelable
import java.lang.IllegalStateException

class User() : Parcelable {
    var userName: String = ""
    var userid: String = ""
    var age: Int = 0

    constructor(name: String, userid: String, age: Int) : this() {
        this.userName = name
        this.userid = userid
        this.age = age
    }

    constructor(parcel: Parcel) : this() {
        userName = parcel.readString() ?: throw IllegalStateException("should not received a null string")
        userid = parcel.readString() ?: throw IllegalStateException("should not received a null string")
        age = parcel.readInt()
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(userName)
        parcel.writeString(userid)
        parcel.writeInt(age)
    }

    fun readFromParcel(parcel: Parcel){
        userName = parcel.readString() ?: throw IllegalStateException("should not received a null string")
        userid = parcel.readString() ?: throw IllegalStateException("should not received a null string")
        age = parcel.readInt()
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<User> {
        override fun createFromParcel(parcel: Parcel): User {
            return User(parcel)
        }

        override fun newArray(size: Int): Array<User?> {
            return arrayOfNulls(size)
        }
    }


}